/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newkfp;

import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pojos.Treausry;

/**
 *
 * @author labib.farag
 */
@Stateless
public class TreausryFacade extends AbstractFacade<Treausry> {

    @PersistenceContext(unitName = "KFPNEWPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TreausryFacade() {
        super(Treausry.class);
    }

    public Double findSum(Date date1, Date date2) {

        Query query = null;

        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String queryDate1 = sdf.format(date1);
        String queryDate2 = sdf.format(date2);
        query = em.createNativeQuery("select sum(money_amount) from treausry where action_date between '" + queryDate1 + "' and '" + queryDate2 + "'");
        Double total = (Double) query.getSingleResult();
        if (total == null) {
            query = em.createNativeQuery("select sum(money_amount) from treausry where action_date=CURDate()");
            total = (Double) query.getSingleResult();
        }
        System.out.println("//////Total: " + total);
        return total;
    }

    public void removeTreasuryByBill(Bill bill) {
        try {
            Query query = em.createNativeQuery("delete from treausry where bill_id = " + bill.getIdBill());
            query.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
